using System;
using System.Drawing;
using System.Linq;
using System.Text;
using Android.Provider;
using Newtonsoft.Json;
namespace jivemessenger
{
    public static class StaticMethods
    {
        public static string GetUniqueId()
        {
            //byte[] b = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
            //string str = Convert.ToBase64String(b);
            
            return "android"+Settings.Secure.GetString(StaticSettings.GlobalContext.ContentResolver,Settings.Secure.AndroidId);
        }
        public static string EncodePassword(string password)
        {
            var sha1 = new System.Security.Cryptography.SHA1Managed();
            return string.Join("", sha1.ComputeHash(Encoding.UTF8.GetBytes(password)).Select(x => x.ToString("X2"))).ToLower();
        }
        public static string ObjectToString<T>(object input)
        {

           // MemoryStream stream = new MemoryStream();
         //   DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        //    ser.WriteObject(stream, input);
          //  byte[] bytes = stream.ToArray();
          //  return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            return JsonConvert.SerializeObject(input);
        }
        public static ChatBubbleCollection StringToChatBubbleCollection(string input)
        {
            //if (String.IsNullOrEmpty(input))
            //    return new ContactEmoticon();
            //MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
            //DataContractJsonSerializer ser = null;
            //try
            //{
            //    ser = new DataContractJsonSerializer(typeof(ContactEmoticon));
            //}
            //catch (Exception e)
            //{
            //    MessageBox.Show(e.ToString());
            //}
            //return (ContactEmoticon)ser.ReadObject(stream);
            return JsonConvert.DeserializeObject<ChatBubbleCollection>(input);
        }


        public static Color ColorFromHex(String hex)
        {
            if (String.IsNullOrEmpty(hex))
                return Color.Black;
            //remove the # at the front
            hex = hex.Replace("#", "");

            byte a = 255;
            byte r = 255;
            byte g = 255;
            byte b = 255;

            int start = 0;

            //handle ARGB strings (8 characters long)
            if (hex.Length == 8)
            {
                a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
                start = 2;
            }

            //convert RGB characters to bytes
            r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
            g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
            b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

            return Color.FromArgb(a, r, g, b);
        }
    }
}